﻿using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using Microsoft.TeamFoundation.Client;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MessageBox = System.Windows.MessageBox;
using UserControl = System.Windows.Controls.UserControl;

namespace TfsComLib.ItemBrowser
{
    /// <summary>
    ///     Interaction logic for VcItemBox.xaml
    /// </summary>
    public partial class VcItemBox : UserControl
    {
        public VcItemBox()
        {
            InitializeComponent();
        }

        private void ClickBrowse(object sender, RoutedEventArgs e)
        {
            try
            {
                using (var cid = new ChooseItemDialog(ProjectCollection, VcItemPath))
                {
                    cid.ShowDialog();
                    if (cid.DialogResult == DialogResult.OK)
                    {
                        VcItemPath = cid.GetSelectedItem();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ChooseItemDialog error");
            }
        }

        private void box_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                VcItemPath = box.Text;
            }
        }

        #region DependencyProperty VcItemPath VcItemPathProperty

        public static readonly DependencyProperty VcItemPathProperty =
            DependencyProperty.Register("VcItemPath", typeof (string),
                typeof (VcItemBox),
                new FrameworkPropertyMetadata(string.Empty,
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                    OnVcItemPathChanged));

        private static void OnVcItemPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as VcItemBox;
            if (control == null) return;
            control.box.Text = e.NewValue as string;
        }

        /// <summary>
        ///     VcItemPath property by Deperndency property
        /// </summary>
        public string VcItemPath
        {
            get { return (string) GetValue(VcItemPathProperty); }
            set { SetValue(VcItemPathProperty, value); }
        }

        #endregion

        #region DependencyProperty TfsTeamProjectCollection ProjectCollectionProperty

        public static readonly DependencyProperty ProjectCollectionProperty =
            DependencyProperty.Register("ProjectCollection", typeof (TfsTeamProjectCollection),
                typeof (VcItemBox),
                new FrameworkPropertyMetadata(null,
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                    OnProjectCollectionChanged));

        private static void OnProjectCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as VcItemBox;
            if (control == null) return;
            control.box.Text = e.NewValue as string;
        }

        /// <summary>
        ///     ProjectCollection property by Deperndency property
        /// </summary>
        public TfsTeamProjectCollection ProjectCollection
        {
            get { return (TfsTeamProjectCollection) GetValue(ProjectCollectionProperty); }
            set { SetValue(ProjectCollectionProperty, value); }
        }

        #endregion
    }
}